using CoreManager.API.Common;
using CoreManager.Core.Captcha;
using CoreManager.Models.DTOs;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;

namespace CoreManager.API.Controllers
{
    /// <summary>
    /// 验证码控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    [AllowAnonymous] // 验证码接口不需要鉴权
    public class CaptchaController : BaseController
    {
        private readonly ICaptchaService _captchaService;
        private readonly ILogger<CaptchaController> _logger;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="captchaService">验证码服务</param>
        /// <param name="logger">日志服务</param>
        public CaptchaController(ICaptchaService captchaService, ILogger<CaptchaController> logger) : base(logger)
        {
            _captchaService = captchaService;
            _logger = logger;
        }

        /// <summary>
        /// 获取验证码
        /// </summary>
        /// <returns>验证码信息</returns>
        [HttpGet]
        public IActionResult GetCaptcha()
        {
            try
            {
                var captchaResult = _captchaService.GenerateCaptcha();
                
                var result = new CaptchaDto
                {
                    RequestId = captchaResult.RequestId,
                    CaptchaImage = captchaResult.CaptchaImage,
                    ExpiresAt = captchaResult.ExpiresAt
                };
                
                return Success(result, "获取验证码成功");
            }
            catch (Exception ex)
            {
                return HandleException(ex, "获取验证码");
            }
        }
    }
}